Repository navigation
feat(agents): 资源库改写 Agent 文件前也先备份,恢复覆盖资源库与供应商的全部修改 - #260
Conversation
- agentLibrary.apply 写 MCP 服务器、Hooks 和整份指令文件(含 replace 覆盖)前先备份,备份失败不写
- 同一个文件不论资源库还是供应商先改,只留一份基线
- agentLibrary.read 的每个 Agent 带 backup {savedAtMs, modifiedSince},与 agentProviders.read 相同
- agentProviders.restore 不再要求声明 providers;放回指令文件前先忘掉 Core 记的指令哈希
- 契约、领域文档、Core API 文档与协议更新记录同步;agents 0 内的补充,不升版本
Refs 1lck/Lithe-IDEA#2654
…ckups # Conflicts: # shared/plugins/reference/api-catalog.md # shared/plugins/reference/protocol-changelog.md
| let instructions = paths | ||
| .instructions | ||
| .as_deref() | ||
| .map(|path| paths.backup_id(path, INSTRUCTIONS_BACKUP_ID)); | ||
| let _writing = self.lock_writing(); | ||
| restore( | ||
| &config, | ||
| &key, | ||
| &paths.backed_up(), | ||
| request.discard_later_changes, | ||
| |restoring| { |
There was a problem hiding this comment.
🔴 Restore overwrites a different instruction file
After an agent’s home changes, restore_backup can apply the old home’s baseline to the new instruction file. Writing instructions in the new home updates the same backup’s digest, so restore overwrites the new file without warning.
Learn more
An agent can declare an environment-specific instructions path with a fallback, as in the example agent. resolve_root selects the path from the current environment. apply_instructions keys backups by the agent and instruction ID, not that path. take keeps the first baseline for that ID but refreshes the digest on each write. restore checks the current path against that new digest and then uses the old path’s baseline.
Example: Apply library instructions over “Personal rules” in the default home. Set the CLI home override to a second directory containing “Work rules”, then apply library instructions there. A normal restore writes “Personal rules” to the work directory instead of restoring “Work rules”.
Recommended fix: Persist the resolved target identity with each baseline. Keep separate baselines for separate targets and never restore one into a different target, even with discardLaterChanges. Test both a changed environment root and a switch back to the original root.
Was this helpful? React with 👍 or 👎 to provide feedback.
| let (agent, file) = backup; | ||
| let taken = backups::take(config, agent, file, now.as_deref(), &edited)?; |
There was a problem hiding this comment.
🔴 Restore silently discards intervening settings edits
After a CLI settings edit, edit_staged can carry it into a library write and mark the whole file unchanged. Restore then rolls back the CLI edit without discard confirmation.
Learn more
The plugin edits a copy and keeps unrelated settings, as the configuration edit contract requires. If the same file already has a baseline, take leaves that baseline intact but replaces the recorded digest with the digest of the entire new file. restore compares only that digest before putting the baseline back. Before this PR a library write left the provider backup’s digest unchanged, so restore still required confirmation.
Example: Change the model in Lithe, enable a logging setting in the CLI, then add an MCP server in the library. The MCP edit keeps logging enabled, but restore now accepts the default request and disables logging along with undoing the model and MCP edits.
Recommended fix: Detect divergence from an existing backup’s last-write digest before advancing it. Preserve a separate indication that restoring the baseline would discard outside changes and honor it in both read status and restore until the baseline is retired.
Was this helpful? React with 👍 or 👎 to provide feedback.
| let agent_key = agent.record_key(); | ||
| // The same ID a library change uses for this file, so the file keeps | ||
| // one baseline whichever kind of change came first. | ||
| let backup_id = paths.backup_id(&target_path, &target_id); |
There was a problem hiding this comment.
🔴 Duplicate baselines break upgraded restores
When two IDs name one file, backup_id can bypass an existing backup under the second ID after upgrade. Restore rejects the conflicting digests or, when forced, can finish with the newer baseline.
Learn more
check_configuration requires distinct IDs, not distinct paths. Before this PR, provider edits recorded the ID returned by the plugin. The new path-based selection always takes the first matching declaration. take treats an unused ID as a new baseline, even if another ID already backs up that path. restore checks and applies both entries in key order, then removes them.
Example: A manifest lists z-main first and a-model second, both naming the same file. Before upgrade, changing model A to B under a-model saves A. After upgrade, changing B to C under the new z-main key saves B. Normal restore rejects the old digest for B; forced restore writes A and then B, and consumes both baselines.
Recommended fix: Look up existing backup records by resolved target before assigning the first declaration’s ID to a new write. If multiple records already refer to one target, reconcile them before any restore write; restore a target at most once and retain the earliest valid baseline.
Was this helpful? React with 👍 or 👎 to provide feedback.
| let (agent, file) = backup; | ||
| let taken = backups::take(config, agent, file, now.as_deref(), &edited)?; |
There was a problem hiding this comment.
🔴 CLI edits lost during backup
If the CLI saves settings during backups::take, edit_staged can overwrite them without a fresh file check. The backup holds the earlier bytes, so it cannot recover that save.
Learn more
edit_staged checks that the original snapshot still matches before it takes the new backup. Its mutex serializes only Lithe’s library commands. take snapshots those already-read bytes and synchronously writes backup data and the record. An independent CLI can save after the check, and the final rename replaces that save. apply_instructions adds the same backup window between reading the file and replacing it.
Example: Turn on an MCP server in Lithe while the CLI saves a preference. If the CLI save completes during backup sync, the staged file wins and neither the baseline nor the history for that write contains the preference.
Recommended fix: Revalidate the target and original bytes after the backup and as close to the rename as possible, undoing the pending backup on a mismatch. Apply the same check to instructions. A strict guarantee against arbitrary external writers requires a conditional replacement or a lock those writers honor.
Was this helpful? React with 👍 or 👎 to provide feedback.
关联
agentProviders.apply的修改做了备份,这里把资源库的修改也纳入同一份备份改动
都是 API-45、
agents能力 0 内的补充,不升版本。没有新增错误码、日志事件、宿主方法、通知或插件函数参数,插件后端协议不变,四个 SDK 不需要改。一、资源库的修改也先备份
agentLibrary.apply改写这三类文件前先备份:replace: true覆盖不是 Lithe 写的内容的那一次。agentProviders.backupFailed(语义相同)。二、一个文件只有一份基线
lithe:instructions。config.toml同时有 MCP 服务器和模型供应商。三、宿主看到的状态和恢复入口
agentLibrary.read的每个已授权 Agent,有备份时多一个可选字段backup {savedAtMs, modifiedSince},和agentProviders.read的backup完全相同。后者现在也覆盖指令文件和资源库的修改。agentProviders.restore不再要求 Agent 声明providers(原来回答agentProviders.notDeclared),恢复这个 Agent 所有备份过的文件。参数、返回值和其余错误码不变。agentLibrary.restore:备份按 Agent 存,一份备份对应一个恢复入口;另加一个等于同一个操作有两个名字。四、恢复后状态和磁盘一致
state.json里这个 Agent“最后写入的指令哈希”;删不掉就整次恢复失败,一个文件都不写。instructions.state只按文件实际内容判断,不会显示written(已是最新);内容和资源库不一样时显示other,再写入要重新确认replace: true。五、文档
shared/plugins/contract/v1/README.md:backups/;agents[].backup;服务器和 Hooks 的写入步骤加“先备份”;技能不备份;指令写入先备份。providers.read那段补上小标题,它原来误挂在 Backups 标题下。shared/plugins/reference/domains/agents.md中英各加“修改前备份与恢复”;rust/contracts/rust-core-api.md三行;API 总表 API-45 描述、能力版本表agents0 两行;协议更新记录加一行。新旧搭配
LibraryAgent没有backup字段,会忽略它,资源库页照旧;供应商页的“恢复原配置”会连指令文件和资源库的修改一起恢复;只有资源库的 Agent 在宿主接入前看不到恢复入口。agentLibrary.read没有backup,宿主不显示恢复。桌面端接入在 1lck/Lithe-IDEA#2654 的后续工作包里做,并在那时升级固定的 Core 提交。
已知限制
other。#249 引入的两个恢复问题(恢复中途存历史失败会留下找不到基线的记录;备份 ID 解析不到路径时恢复永远失败、提示无法清除)不在本 PR 修,已建单 1lck/Lithe-IDEA#2656。
验证(macOS arm64 本机)
cargo test -p lithe-agent-runtimetests::library::backups5 个cargo test --locked --workspace --features lithe-remote/server,lithe-remote/motty,lithe-ide-host/mcp(CI 的 Linux 参数)node scripts/rust-core/verify-platform-tests.mjsnode --test scripts/rust-core/*.test.mjscargo fmt --all -- --checkcargo clippy --locked --workspace --all-targets --all-features -- -D warnings-p lithe-agent-runtime -p lithe-core)node scripts/logging/generate-log-events.mjs --checkscripts/rust-core/verify-rust-core-comments.sh agent-runtimenode shared/plugins/scripts/verify-docs.mjs新增测试覆盖:
modifiedSince的变化与discardLaterChanges;replace覆盖前的内容可以恢复。测试同时检查日志里没有路径、命令和文本。
没有执行的:
sdk-e2e.mjs。